package killerqueen.game.objects;

import killerqueen.game.interfaces.GameObject;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Polygon;

public class RotatingCube implements GameObject {

	private PolygonShape box;
	private Body body;
	private float minAngularSpeed;
	private float maxAngularSpeed;
	private float angularIncrease;
	private float w;
	private float h;
	private float x;
	private float y;

	public RotatingCube() {
	}

	@Override
	public void init(World world, String id) {

		BodyDef bodyDef = new BodyDef();
		bodyDef.position.set(x, y);
		bodyDef.type = BodyType.KINEMATIC;
		body = world.createBody(bodyDef);

		// BodyDef auxBodyDef = new BodyDef();
		// auxBodyDef.position.set(posX, posY);
		// Body auxBody = world.createBody(auxBodyDef);

		box = new PolygonShape();
		box.setAsBox(w, h);

		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = box;
		fixtureDef.density = 5;
		fixtureDef.friction = 1f;
		fixtureDef.restitution = 0.1f;

		body.createFixture(fixtureDef);
		body.setAngularVelocity(minAngularSpeed);
		// RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
		// revoluteJointDef.localAnchorA = new Vec2(0,0);
		// revoluteJointDef.localAnchorB = new Vec2(0,0);
		// revoluteJointDef.bodyA = body;
		// revoluteJointDef.bodyB = auxBody;
		// revoluteJointDef.collideConnected = false;
		// revoluteJointDef.enableLimit = false;
		// revoluteJointDef.enableMotor = true;
		// revoluteJointDef.maxMotorTorque = 5;
		// revoluteJointDef.motorSpeed = 10;
		// world.createJoint(revoluteJointDef);
	}

	@Override
	public void input(Input input) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(long delta) {
		if (body.getAngularVelocity() < minAngularSpeed
				|| body.getAngularVelocity() > maxAngularSpeed)
			angularIncrease = -angularIncrease;

		body.setAngularVelocity(body.getAngularVelocity()
				+ body.getAngularVelocity() * angularIncrease);

	}

	@Override
	public void paint(Graphics g) {
		g.pushTransform();

		Polygon p = new Polygon();

		Vec2 pos = body.getPosition();
		float angle = body.getAngle();
		g.translate(pos.x, pos.y);

		g.rotate(0, 0, (float) Math.toDegrees(+angle));

		for (int i = 0; i < box.getVertexCount(); i++) {
			Vec2 vertex = box.getVertex(i);
			p.addPoint(vertex.x, vertex.y);
		}

		g.setColor(Color.blue);
		g.fill(p);
		g.setColor(Color.black);
		g.draw(p);

		g.popTransform();
	}

	public void setW(float w) {
		this.w = w;
	}

	public void setH(float h) {
		this.h = h;
	}

	public void setX(float x) {
		this.x = x;
	}

	public void setY(float y) {
		this.y = y;
	}

	public void setMinAngularSpeed(float minAngularSpeed) {
		this.minAngularSpeed = minAngularSpeed;
	}

	public void setMaxAngularSpeed(float maxAngularSpeed) {
		this.maxAngularSpeed = maxAngularSpeed;
		System.out.println(this.maxAngularSpeed);
	}

	public void setAngularIncrease(float angularIncrease) {
		this.angularIncrease = angularIncrease;
		System.out.println(this.angularIncrease);
	}

	@Override
	public void destroy(World world) {
		// TODO Auto-generated method stub
		
	}
}
